package com.cnhis.cloudhealth.clinical.hosdoctor.doctorparameter.bo;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cnhis.cloudhealth.clinical.hosdoctor.doctorparameter.dao.DoctorparameterDao;
import com.cnhis.cloudhealth.commons.Mappers.ModelVo;
import com.cnhis.cloudhealth.commons.newid.GetNewId;
import com.cnhis.cloudhealth.commons.utils.StaticKeys;
import com.cnhis.cloudhealth.commons.utils.StringUtils;

@Service
public class DoctorparameterBo {
	@Resource
	private DoctorparameterDao doctorparameterDao;
	@Autowired
	private GetNewId getNewId;

	private ModelVo packModelVo(String nid,Map map){
		ModelVo mo = new ModelVo();
		List<?> list = null;
		try {
			list = doctorparameterDao.queryForList(nid,map);
		} catch (Exception e) {
			e.printStackTrace();
			mo.setResult(StaticKeys.OH_FALSE);
		}
		if(list!=null&&list.size()>0) {
			mo.setList(list);
			mo.setResult(StaticKeys.OH_SUCCESS);
			mo.setTotal(list.size());
			mo.setRows(list);
		}
		return mo;
	}

	public ModelVo selectPersonalParams(Map<String,Object> map){
		return packModelVo("Doctorparameter.getPersonalParams",map);
	}
	public ModelVo selectDepartmentParams(Map<String,Object> map){
		return packModelVo("Doctorparameter.getDepartmentParams",map);
	}
	public ModelVo selectPublicParams(Map<String,Object> map){
		return packModelVo("Doctorparameter.getPublicParams",map);
	}
	
	public String getSysValue(int productId,int programId,int paramNo )throws Exception{
		Map<String,Object> map = new HashMap();
		map.put("productid",productId);
		map.put("programid",programId);
		map.put("paramno", paramNo);
		return (String)doctorparameterDao.queryToObject("Doctorparameter.getSysParamValue",map);
	}
	public String getUserValue(int productId,int programId,int paramNo,long userId,int privilege)throws Exception{
		Map<String,Object> map = new HashMap();
		map.put("productid",productId);
		map.put("programid",programId);
		map.put("paramno", paramNo);
		map.put("userid", userId);
		map.put("privilege", privilege);
		return (String)doctorparameterDao.queryToObject("Doctorparameter.getUserParamValue",map);
	}

	public Boolean savedOneUserParameters(Map map)throws Exception{
		Boolean result = false;
		Integer cnt1 = (Integer)doctorparameterDao.queryForExtObject("Doctorparameter.soup_001",map);	//判断是否存在此系统参数
		Integer privilege = MapUtils.getInteger(map,"privilege");
		map.put("pid", getNewId.nextId());
		map.put("uid", getNewId.nextId());
		if (cnt1==0){
			doctorparameterDao.insert("Doctorparameter.soup_002",map);		//插入系统参数
			if(privilege==1||privilege==2||privilege==3){
				doctorparameterDao.insert("Doctorparameter.soup_003",map);	//插入用户参数
			}
			result = true;
		}else{
			String pvalue = MapUtils.getString(map,"pvalue");
			if (privilege == 0) {
				String psvalue = (String)doctorparameterDao.queryForExtObject("Doctorparameter.getSysParamValue",map);
				if(psvalue==null||!StringUtils.isEquals(psvalue,pvalue)) {	//如果和原来参数一致无需更新
					Integer cnt4 = (Integer) doctorparameterDao.queryForExtObject("Doctorparameter.soup_004", map); //根据权限类型判断是否存在此系统参数
					if (cnt4 > 0 && privilege == 0) {
						doctorparameterDao.delete("Doctorparameter.soup_005", map);  //删除相关用户参数
					}
					Map<String, Object> pmap1 = (Map<String, Object>) doctorparameterDao.queryForExtObject("Doctorparameter.soup_007", map); //更改操作参数1
					if (pmap1 != null) {
						map.put("empname", MapUtils.getString(pmap1, "empname"));
						map.put("host", MapUtils.getString(pmap1, "host"));
						map.put("ip", MapUtils.getString(pmap1, "ip"));
					}
					Map<String, Object> pmap2 = (Map<String, Object>) doctorparameterDao.queryForExtObject("Doctorparameter.soup_008", map); //更改操作参数2
					if (pmap2 != null) {
						map.put("programname", MapUtils.getString(pmap2, "programname"));
						map.put("event", MapUtils.getString(pmap2, "event"));
					}
					long eventlogid = getNewId.nextId();
					map.put("eventlogid", eventlogid);
					map.put("messageid", 0);
					doctorparameterDao.insert("Doctorparameter.soup_009", map);    //插入更改参数日志记录
					doctorparameterDao.update("Doctorparameter.soup_010", map);    //更新系统参数
					result = true;
				}
			}else if (privilege == 1 || privilege == 2 || privilege == 3) {
				String uvalue = (String)doctorparameterDao.queryForExtObject("Doctorparameter.getUserParamValue",map);
				if(uvalue==null||!StringUtils.isEquals(uvalue,pvalue)) {    //如果和原来参数一致则无需更新
					doctorparameterDao.delete("Doctorparameter.soup_006", map);    //根据用户id删除用户参数
					Integer cnt10 = (Integer) doctorparameterDao.queryForExtObject("Doctorparameter.soup_011", map);
					if (cnt10 == 0) {
						doctorparameterDao.insert("Doctorparameter.soup_012", map);    //更新系统参数
						result = true;
					} else {
						doctorparameterDao.update("Doctorparameter.soup_010", map);    //插入更改参数日志记录
						result = true;

					}
				}
			}
		}
		return result;
	}
	public ModelVo selectOrderstatusdesc(){
		return packModelVo("Doctorparameter.7711",null);
	}

	public void updateOrderDesc(Map<String,Object> map)throws Exception{
		doctorparameterDao.update("Doctorparameter.updateOrderDesc",map);
	}
	public ModelVo selectOrderFields(){
		return packModelVo("Doctorparameter.getOrderFields",null);
	}


	public String getiah04(Map<String,Object> map)throws Exception{
		return (String)doctorparameterDao.queryToObject("Doctorparameter.getiah04",map);
	}









}
